package cn.edu.hitwh.kanlema.util;

import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExpirationWrapper {
    private String value;
    private LocalDateTime expireTime;

    public ExpirationWrapper(String value, long timeout, TimeUnit unit) {
        this.value = value;
        this.expireTime = LocalDateTime.now().plusNanos(unit.toNanos(timeout));
    }

    public static ExpirationWrapper asJson(Object value, long timeout, TimeUnit unit) {
        return new ExpirationWrapper(JSONUtil.toJsonStr(value), timeout, unit);
    }

    public <T> T getJsonAsObject(Class<T> clazz) {
        return JSONUtil.toBean(this.value, clazz);
    }
}
